import Joi from 'joi'
import { Result } from '~~/lib/helper/result'
import prisma from '~~/lib/prisma'

export default defineEventHandler(async (event) => {
  const body = await getQuery<{ id: number }>(event)
  const schema = Joi.object({
    id: Joi.number().required(),
  })

  try {
    await schema.validateAsync(body)
  } catch (error: any) {
    return Result.Error('参数错误！' + error.message)
  }
  try {
    // 删除文章
    await prisma.note.delete({ where: { id: +body.id } })
    return Result.Ok(null, 200, '删除文章成功！')
  } catch (error) {
    console.log('error', error)
    return Result.Error('删除文章失败！')
  }
})
